package com.newenergy.wisehome.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.callback.PageHelperListener;
import com.newenergy.wisehome.common.util.ImageUtil;
import com.newenergy.wisehome.common.util.LocaleUtil;
import com.newenergy.wisehome.data.bean.PageBean;
import com.newenergy.wisehome.data.bean.PageData;
import com.newenergy.wisehome.ui.activity.VideoHelpActivity;
import com.newenergy.wisehome.ui.widget.GlideViewPager;
import com.newenergy.wisehome.ui.widget.ZoomIndicator;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.yokeyword.fragmentation.ISupportFragment;
import uk.co.senab.photoview.PhotoView;

/**
 * Created by new on 2017/12/1.
 * 帮助界面
 */

public class HelpFragment extends BaseFragment {

    @BindView(R.id.loop_viewpager_text)
    GlideViewPager viewPager;
    @BindView(R.id.bottom_text_layout)
    ZoomIndicator zoomIndicator;

    private boolean isOnResume=true;

    private List<PageData> mImageViews = new ArrayList<>();


    @Override
    public int getLayoutId() {
        return R.layout.fragment_help;
    }

    public static ISupportFragment newInstance() {
        return new HelpFragment();
    }

    @Override
    public void init(View rootView) {
//        mTvPage.setText(getString(R.string.use_guide));

    }


    @Override
    public void onResume() {
        super.onResume();
        if (isOnResume) {
            if (LocaleUtil.getLanguage().startsWith("zh")) {
                initImageViewData();
            } else {
                initImageViewEnData();
            }

            //配置pagerbean，这里主要是为了viewpager的指示器的作用，注意记得写上泛型
            PageBean bean = new PageBean.Builder<PageData>()
                    .setDataObjects(mImageViews)
                    .setIndicator(zoomIndicator)
                    .builder();


            viewPager.setPageListener(bean, R.layout.help_image_item, new PageHelperListener() {
                @Override
                public void getItemView(View view, Object data) {

                    PhotoView imageView = (PhotoView) view.findViewById(R.id.iv_oper_guide);
                    Button linkHlep = (Button) view.findViewById(R.id.link_help);

                    Button btnBack = (Button) view.findViewById(R.id.btn_back);
                    Button btnPlayVideo = (Button) view.findViewById(R.id.btn_playVideo);
                    TextView tvBack = (TextView) view.findViewById(R.id.tv_back);

                    TextView ivTitle = (TextView) view.findViewById(R.id.iv_title);

               /* ImageView toLeft = (ImageView) view.findViewById(R.id.iv_to_left);
                ImageView toRight = (ImageView) view.findViewById(R.id.iv_to_right);*/


                    PageData bean = (PageData) data;

                    int photoViewId = ImageUtil.getImageId(bean.getPhotoView());

                    Glide.with(mainActivity).load(photoViewId).into(imageView);

                    ivTitle.setText(bean.getTitle());

                    btnBack.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            doBack();
                        }
                    });

                    tvBack.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            doBack();
                        }
                    });


                    if (bean.getPhotoView().equals("guide_light_img")/*||bean.getPhotoView().equals("guide_hea_img")*/||bean.getPhotoView().equals("guide_remote_img")||bean.getPhotoView().equals("guide_light_reset_img")
                            ||bean.getPhotoView().equals("guide_light_img_en")/*||bean.getPhotoView().equals("guide_hea_img_en")*/||bean.getPhotoView().equals("guide_remote_img_en")||bean.getPhotoView().equals("guide_light_reset_img_en")) {
                        btnPlayVideo.setVisibility(View.VISIBLE);
                    }else {
                        btnPlayVideo.setVisibility(View.GONE);
                    }



                    btnPlayVideo.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            if (bean.getPhotoView().equals("guide_light_img") || bean.getPhotoView().equals("guide_light_img_en")) {
                                Intent intent = new Intent(getContext(), VideoHelpActivity.class);
                                intent.putExtra("VIDEOURL", "http://apps.wisewish.cn/video/wiseHomeLight.html");
                                startActivity(intent);

                            } else if (bean.getPhotoView().equals("guide_hea_img") || bean.getPhotoView().equals("guide_hea_img_en")) {
                                Intent intent = new Intent(getContext(), VideoHelpActivity.class);
                                intent.putExtra("VIDEOURL", "http://apps.wisewish.cn/video/wiseHomeHEA.html");
                                startActivity(intent);

                            } else if (bean.getPhotoView().equals("guide_remote_img") || bean.getPhotoView().equals("guide_remote_img_en")) {
                                Intent intent = new Intent(getContext(), VideoHelpActivity.class);
                                intent.putExtra("VIDEOURL", "http://apps.wisewish.cn/video/wiseHomeRemoteShare.html");
                                startActivity(intent);

                            }else if (bean.getPhotoView().equals("guide_light_reset_img") || bean.getPhotoView().equals("guide_light_reset_img_en")) {
                                Intent intent = new Intent(getContext(), VideoHelpActivity.class);
                                intent.putExtra("VIDEOURL", "http://apps.wisewish.cn/video/wiseHomeLightReset.html");
                                startActivity(intent);

                            }



                        }
                    });

                }
            });
        }
    }


    @Override
    public void onPause() {
        super.onPause();
        isOnResume = false;
    }

    private void initImageViewEnData() {
        PageData pageData = new PageData("guide_light_img_en", "Light Guide");
        PageData pageData1 = new PageData("guide_hea_img_en", "HEA Guide");
        PageData pageData2 = new PageData("guide_remote_img_en", "Remote Guide");
        PageData pageData3 = new PageData("guide_light_reset_img_en", "Restore Settings");

        mImageViews.add(pageData);
        mImageViews.add(pageData1);
        mImageViews.add(pageData2);
        mImageViews.add(pageData3);
    }

    private void initImageViewData() {
        PageData pageData = new PageData("guide_light_img", "照明指引");
        PageData pageData1 = new PageData("guide_hea_img", "家电指引");
        PageData pageData2 = new PageData("guide_remote_img", "遥控器分享指引");
        PageData pageData3 = new PageData("guide_light_reset_img", "恢复出厂设置");

        mImageViews.add(pageData);
        mImageViews.add(pageData1);
        mImageViews.add(pageData2);
        mImageViews.add(pageData3);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        mImageViews.clear();
        mImageViews = null;
    }


}
